using System;
using System.Collections.Generic;
using System.Text;
using TALoaders;

namespace TALoaders.Tests
{
    using NUnit.Framework;

    [TestFixture]
    class TestGoogleDownloader
    {
        private GoogleDownloader googleLoaderM = new GoogleDownloader();
        private YahooDownloader yahooLoaderM = new YahooDownloader();

        [Test]
        [Category("DataCollection")]
        public void getHistoricalData()
        {
            // Test AdjClose
            SortedList<DateTime, double> close = googleLoaderM.getHistoricalPrices("NYSE:USO", new DateTime(2010, 1, 19), new DateTime(2010, 2, 17), QuoteType.Close);
            Assert.AreEqual(37.24, close.Values[2], "Testing NYSE:USO Close");

            // Test Low
            SortedList<DateTime, double> lows = googleLoaderM.getHistoricalPrices("NYSE:USO", new DateTime(2010, 1, 19), new DateTime(2010, 2, 17), QuoteType.Low);
            double totalLow = 0;
            foreach (double dailyLow in lows.Values)
                totalLow += dailyLow;
            Assert.AreEqual(796.42, totalLow, "Testing NYSE:USO LOW"); // Actually wrong, it adds also low from Feb 15th, when volume was = 0

            // Test Open
            SortedList<DateTime, double> open = googleLoaderM.getHistoricalPrices("NYSE:USO", new DateTime(2010, 1, 19), new DateTime(2010, 2, 17), QuoteType.Open);
            Assert.AreEqual(open.Values[1], 38.23, "Testing NYSE:USO OPEN");
        }

        [Test]
        [Category("DataCollection")]
        public void getHistoricalDataAllQuoteTypes()
        {
            string ticker = "NYSE:USO";
            SortedList<DateTime, Quote> quotes = googleLoaderM.getHistoricalPrices(ticker, new DateTime(2010, 1, 19), new DateTime(2010, 1, 22));
            Quote quote = quotes[new DateTime(2010, 1, 20)];
            Assert.AreEqual(38.23, quote.getValue(QuoteType.Open), "OPEN");
            Assert.AreEqual(38.35, quote.getValue(QuoteType.High), "HIGH");
            Assert.AreEqual(37.88, quote.getValue(QuoteType.Low), "LOW");
            Assert.AreEqual(37.89, quote.getValue(QuoteType.Close), "CLOSE");
            Assert.AreEqual(11444792, quote.getValue(QuoteType.Volume), "VOLUME");
            Assert.AreEqual(37.89, quote.getValue(QuoteType.AdjClose), "AdjClose");
            Assert.AreEqual(0.5 * (38.35 + 37.88), quote.getValue(QuoteType.AvgHL), "(H+L)/2");
            Assert.AreEqual((38.35 + 37.88 + 37.89) / 3.0, quote.getValue(QuoteType.AvgHLC), "(H+L+C)/3");
        }

        [Test]
        [Category("DataCollection")]
        public void getLastPrice()
        {
            string ticker = "NYSE:BRK.B";
            DateTime start = DateTime.Now;
            start = start.AddDays(-5);

            SortedList<DateTime, double> history = googleLoaderM.getHistoricalPrices(ticker, start, DateTime.Now, QuoteType.Close);
            double lastHistory = history.Values[history.Count - 1];
            string timeStamp;
            double lastPrice = googleLoaderM.getLastPrice(ticker, out timeStamp);
            Assert.Less(Math.Abs(lastHistory - lastPrice) / lastPrice, 0.2); // Assume that moves less than 20% in a day
        }

        [Test]
        [Category("DataCollection")]
        public void getHistoricalDataYahooGoogle()
        {
            string gTicker = "NYSE:KO"; // Coca-Cola
            string yTicker = "KO";
            DateTime startDate = new DateTime(2010, 1, 19);
            DateTime endDate = new DateTime(2010, 2, 5);
            SortedList<DateTime, double> gHistory = googleLoaderM.getHistoricalPrices(gTicker, startDate, endDate, QuoteType.Close);
            SortedList<DateTime, double> yHistory = yahooLoaderM.getHistoricalPrices(yTicker, startDate, endDate, QuoteType.Close);
            Assert.AreEqual(gHistory.Count, yHistory.Count, "Size of Yahoo and Google output for Coca-Cola");

            foreach (KeyValuePair<DateTime, double> p in gHistory)
                Assert.AreEqual(gHistory[p.Key], p.Value, "Different values for " + p.Key.ToString());
        }

        [Test]
        [Category("DataCollection")]
        public void getLastPriceDataYahooGoogle()
        {
            string gTicker = "NYSE:KO"; // Coca-Cola
            string yTicker = "KO";
            float tollerance = 0.12F;
            string timeStamp;
            float gPrice = (float)googleLoaderM.getLastPrice(gTicker, out timeStamp);
            float yPrice = (float)yahooLoaderM.getLastPrice(yTicker, out timeStamp);
            Assert.AreEqual(gPrice, yPrice, tollerance, "Different last prices betwen Google and Yahoo for Coca-Cola");
        }
    }
}
